//
//  OrderAssignViewController.m
//  CloudMD
//
//  Created by centling on 16/4/20.
//  Copyright © 2016年 HuaZhengInfo. All rights reserved.
//

#import "OrderAssignViewController.h"
#import "GrabOrderTableViewCell.h"
#import "AssignDetailViewController.h"
#import "GetOrderListModel.h"

@interface OrderAssignViewController ()<UITableViewDataSource,UITableViewDelegate>{
    
    UITableView * _mainTableView;
    NSMutableArray * _dataArray;
    NSMutableArray * _mainArray;
    NSMutableArray * _detailArray;
    
    NSString * _PAGEINDEX; //  页数
    NSString * _ROWNUM;
    int _pagenums; // 页数
    
}

@property (nonatomic, assign) BOOL pullHeader;

@end

@implementation OrderAssignViewController

- (void)viewWillAppear:(BOOL)animated{
    
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = NO;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    // 初始化
    _dataArray = [NSMutableArray array];
    _mainArray = [NSMutableArray array];
    _detailArray = [NSMutableArray array];
    _PAGEINDEX = @"1";
    _ROWNUM = @"10";
    _pagenums = 1;
    
    [self buildTableView];
    [self setupRefresh];
    
    [_mainTableView headerBeginRefreshing];
    [self requestUserInfo];
    
}

#pragma mark -- 请求数据
- (void)requestData{
    
    [_dataArray removeAllObjects];
    NSArray * keysArray= @[@"USER_ID",@"USER_CODE",@"MODULE",@"TYPE",@"PAGEINDEX",@"ROWNUM"];
    NSArray * valueArray = @[@"41055",USER_CODE,@"",@"",_PAGEINDEX,_ROWNUM];
    NSDictionary * dict = [[NSDictionary alloc] initWithObjects:valueArray forKeys:keysArray];
    NSString * urlStr = [NSString stringWithFormat:@"%@%@",Base_URL,ImposeOrderList_URL];
    //    [SVProgressHUD show];
    //    [SVProgressHUD showWithStatus:@"加载中..."];
    HZNetWorkAgent *agent = [HZNetWorkAgent shareInstance];
    [agent requestUrl:urlStr param:dict baseUrl:@"" withRequestMethod:HZRequestMethodPost withCompletionWithSuccess:^(HZBaseRequest *http, BOOL isSuccess) {
        NSMutableArray * array = [http.returnArray mutableCopy];
        [_mainTableView headerEndRefreshing];
        [_mainTableView footerEndRefreshing];
        
        for (NSDictionary * dataDict in array) {
            
            GetOrderListModel * model = [[GetOrderListModel alloc] initWithDict:dataDict];
            [_dataArray addObject:model];
            
        }
        
        if ([_PAGEINDEX isEqualToString:@"1"]) {
            [_detailArray removeAllObjects];
        }
        for (GetOrderListModel *  model in _dataArray) {
            
            [_detailArray addObject:model];
        }
        _mainArray = [_detailArray copy];
        [_mainTableView reloadData];
    } withTag:101];
    
}

- (void)requestUserInfo{
    
    NSArray * keyArray = @[@"MOBILE"];
    NSArray * valueArray = @[@"18705426875"];
    NSDictionary * dict = [[NSDictionary alloc] initWithObjects:valueArray forKeys:keyArray];
    NSString * urlStr = [NSString stringWithFormat:@"%@%@",Base_URL,Login_URL];
    
    HZNetWorkAgent *agent = [HZNetWorkAgent shareInstance];
    [agent requestUrl:urlStr param:dict baseUrl:@"" withRequestMethod:HZRequestMethodPost withCompletionWithSuccess:^(HZBaseRequest *http, BOOL isSuccess) {
        NSMutableArray * array = [http.returnArray mutableCopy];
        for (NSDictionary * dataDict in array) {
            
            NSString * User_ID = [dataDict objectForKey:@"USER_CODE"];
            NSString * User_SCDE = [dataDict objectForKey:@"USER_ID"];
            
            // 保存数据
            NSUserDefaults * infoDefaults = [NSUserDefaults standardUserDefaults];
            [infoDefaults setObject:User_ID forKey:@"USER_ID"];
            [infoDefaults setObject:User_SCDE forKey:@"USER_CODE"];
        }
    } withTag:101];
}

- (void)buildTableView{
    
    _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCR_W, SCR_H - 64) style:UITableViewStylePlain];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    _mainTableView.rowHeight = 66;
    //    if (iPhone4) {
    //        _mainTableView.rowHeight = SCR_H/9;
    //    }
    //    _mainTableView.tableFooterView = [[UIView alloc] init];
    _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_mainTableView registerNib:[UINib nibWithNibName:@"GrabOrderTableViewCell" bundle:nil] forCellReuseIdentifier:@"GrabOrderCellId"];
    
    [self.view addSubview:_mainTableView];
    
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if (!_mainArray || _mainArray.count == 0) {
        _mainArray = _detailArray;
    }
    return _mainArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    //    NSString * ID = @"GD";
    GrabOrderTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"GrabOrderCellId" forIndexPath:indexPath];
    //    if (cell == nil) {
    //        cell = [[GrabOrderTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    //
    //    }
    
    
    GetOrderListModel * model = _mainArray[indexPath.row];
    cell.model = model;
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.tuoBtn.hidden = YES;
    //    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    //订单号label宽度自适应
    //    CGSize maximumLabelSize = CGSizeMake(1000, cell.detailLabel.frame.size.height);
    //    CGSize expectSize = [cell.sysCodeLabel sizeThatFits:maximumLabelSize];
    //    cell.sysCodeLabel.frame = CGRectMake(cell.sysCodeLabel.frame.origin.x, cell.sysCodeLabel.frame.origin.y, expectSize.width, cell.sysCodeLabel.frame.size.height);
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    AssignDetailViewController * detailVC = [[AssignDetailViewController alloc] init];
    GetOrderListModel * model = _mainArray[indexPath.row];
    detailVC.ID = model.ID;
    detailVC.GUID = model.GUID;
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark --- 刷新
- (void)setupRefresh{
    [_mainTableView addHeaderWithTarget:self action:@selector(headerRefreshing)];
    [_mainTableView addFooterWithTarget:self action:@selector(footerRefreshing)];
    
    //    _mainTableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerRefreshing)];
    //    _mainTableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerRefreshing)];
    
}

- (void)headerRefreshing{
    
    self.pullHeader = YES;
    [_dataArray removeAllObjects];
    _PAGEINDEX = @"1";
    _pagenums = 1;
    [self requestData];
    
}


- (void)footerRefreshing{
    self.pullHeader = NO;
    _pagenums ++;
    [_dataArray removeAllObjects];
    _PAGEINDEX = [NSString stringWithFormat:@"%d",_pagenums];
    [self requestData];
}


@end
